<?php
/**
 * Created by PhpStorm.
 * User: chenxiaogang
 * Date: 2017/12/27
 * Time: 14:42
 */

namespace User\Model;


use Common\Model\CommonModel;

/**
 * Class LoanOperationLog
 * @package     User\Model
 * @description 用户订单贷后操作记录表
 * @end
 */
class LoanOperationLogModel extends CommonModel
{
    /**
     * @var [当前平台]
     *   0 => '诺诺信',
     *   1 => '安厦',
     *   2 => '我来花',
     */
    const CURRENT_PLATFORM = 0;

    /**
     * @var string MySQL连接实例
     */
    protected $connection = 'DB_CONFIG2';
    /**
     * @var string 真实表全名
     */
    protected $trueTableName = 'system_loan_operation_log';

    /**
     * @use          [获取逾期平台标识]
     * @author       chenxiaogang
     * @param $key
     * @return mixed|string
     */
    public static function getPlatformText($key)
    {
        $key = intval( $key );
        $platformList = [
            0 => '诺诺信',
            1 => '安厦',
            2 => '我来花',
        ];
        if(array_key_exists( $key, $platformList )) {
            return $platformList[$key];
        } else {
            return '';
        }
    }

    /**
     * @use          [获取是否解套标识]
     * @author       chenxiaogang
     * @param $key
     * @return mixed|string
     */
    public static function getIsReliefText($key)
    {
        $isReliefList = [
            0 => '否',
            1 => '是',
        ];
        if(array_key_exists( $key, $isReliefList )) {
            return $isReliefList[$key];
        } else {
            return '';
        }
    }

    /**
     * @use          [获取是否续期标识]
     * @author       chenxiaogang
     * @param $key
     * @return mixed|string
     */
    public function getIsRenewalText($key)
    {
        $isRenewalList = [
            0 => '否',
            1 => '是',
        ];
        if(array_key_exists( $key, $isRenewalList )) {
            return $isRenewalList[$key];
        } else {
            return '';
        }
    }

    /**
     * @use          [获取用户状态标识]
     * @author       chenxiaogang
     * @param $key
     * @return mixed|string
     */
    public static function getUserStatusText($key)
    {
        $userStatusList = [
            0 => '待收',
            1 => '完结',
        ];
        if(array_key_exists( $key, $userStatusList )) {
            return $userStatusList[$key];
        } else {
            return '';
        }
    }

    /**
     * @use          [获取是否核对标识]
     * @author       chenxiaogang
     */
    public static function getIsCheckText($key)
    {
        $userStatusList = [
            0 => '否',
            1 => '是',
        ];
        if(array_key_exists( $key, $userStatusList )) {
            return $userStatusList[$key];
        } else {
            return '';
        }
    }

    /**
     * @use          [得到核对列表]
     * @author       chenxiaogang
     * @param $where    array    条件
     * @param $limit    string  限制查询
     * @return mixed    array   返回数组
     */
    public function get_audit_list($where, $limit)
    {
        $fields = [
            'ol.id,ol.oid,ol.user_id,ol.platform,ol.operation_date,ol.is_relief,ol.relief_platform_txt,ol.relief_amount,ol.is_renewal,ol.renewal_amount,ol.user_status,ol.is_check,ol.principal,ol.check_admin_id,ol.check_time',
            'SUM(rl.rev_amount)' => 'total_rev_amount',
            'SUM(IF(pay_way=0,rl.rev_amount,0))' => 'total_system_amount',
            'SUM(IF(pay_way=1,rl.rev_amount,0))' => 'total_wechat_amount',
            'SUM(IF(pay_way=2,rl.rev_amount,0))' => 'total_alipay_amount',
            'SUM(IF(pay_way=3,rl.rev_amount,0))' => 'total_bank_amount',
            'GROUP_CONCAT(IF(pay_way=1,rl.rev_admin_id,null))' => 'wechat_rev_admin_id',
            'GROUP_CONCAT(IF(pay_way=2,rl.rev_admin_id,null))' => 'alipay_rev_admin_id',
        ];
        $data = $this->alias( 'ol' )
            ->field( $fields )
            ->join( 'system_loan_received_log rl ON ol.id=rl.opid', 'left' )
            ->where( $where )
            ->limit( $limit )
            ->group( 'ol.id' )
            ->order( 'ol.id DESC' )
            ->select();
        return $data;
    }

    /**
     * @use          [获取单个贷后的操作统计]
     * @author       chenxiaogang
     * @param $where
     * @return array
     */
    public function get_report_detail_total($where)
    {
        $fields = [
            'ol.relief_amount,ol.renewal_amount',
            'SUM(rl.rev_amount)' => 'total_rev_amount',
        ];
        $data = $this->alias( 'ol' )
            ->field( $fields )
            ->join( 'system_loan_received_log rl ON ol.id=rl.opid', 'left' )
            ->where( $where )
            ->group( 'ol.id' )
            ->select();
        $total = [
            'sum_rev_amount' => 0,
            'total_renewal_amount' => 0,
            'total_relief_amount' => 0,
        ];
        foreach($data as $v){
            $total['sum_rev_amount'] += $v['total_rev_amount'];
            $total['total_renewal_amount'] += $v['renewal_amount'];
            $total['total_relief_amount'] += $v['relief_amount'];
        }
        return $total;
    }

    /**
     * @use          [得到核对列表的条数]
     * @author       chenxiaogang
     * @param $where [条件]
     * @return integer  [返回的记录条数]
     */
    public function get_audit_list_count($where)
    {
        $count = $this->alias( 'ol' )
            ->where( $where )
            ->count( 'ol.id' );
        return $count;
    }

    /**
     * @use          [得到历史操作记录]
     * @author       chenxiaogang
     * @param $where
     * @return mixed
     */
    public function get_list($where)
    {
        $fields = [
            'ol.id,ol.oid,ol.user_id,ol.platform,ol.operation_date,ol.is_relief,ol.relief_platform_txt,ol.relief_amount,ol.is_renewal,ol.renewal_amount,ol.user_status,ol.is_check,ol.principal',
            'SUM(rl.rev_amount)' => 'total_rev_amount',
            'SUM(IF(pay_way=0,rl.rev_amount,0))' => 'total_system_amount',
            'SUM(IF(pay_way=1,rl.rev_amount,0))' => 'total_wechat_amount',
            'SUM(IF(pay_way=2,rl.rev_amount,0))' => 'total_alipay_amount',
            'SUM(IF(pay_way=3,rl.rev_amount,0))' => 'total_bank_amount',
        ];
        $list = $this->alias( 'ol' )
            ->field( $fields )
            ->join( 'system_loan_received_log rl ON ol.id=rl.opid', 'left' )
            ->where( $where )
            ->group( 'ol.id' )
            ->select();
        return $list;
    }

    /**
     * @use          [得到贷后操作统计记录]
     * @author       chenxiaogang
     * @param $where
     * @return mixed
     */
    public function get_report($where)
    {
        $fields = [
            'ol.principal,ol.user_status,ol.is_check,ol.principal,ol.check_admin_id,ol.check_time',
            'SUM(ol.relief_amount)' => 'total_relief_amount',
            'SUM(ol.renewal_amount)' => 'total_renewal_amount',
            'SUM(rl.rev_amount)' => 'total_rev_amount',
            'SUM(IF(pay_way=0,rl.rev_amount,0))' => 'total_system_amount',
            'SUM(IF(pay_way=1,rl.rev_amount,0))' => 'total_wechat_amount',
            'SUM(IF(pay_way=2,rl.rev_amount,0))' => 'total_alipay_amount',
            'SUM(IF(pay_way=3,rl.rev_amount,0))' => 'total_bank_amount',
        ];
        $data = $this->alias( 'ol' )
            ->field( $fields )
            ->join( 'system_loan_received_log rl ON ol.id=rl.opid', 'left' )
            ->where( $where )
            ->group( 'ol.principal' )
            ->select();
        return $data;
    }

    /**
     * @use          [得到贷后统计记录的条数]
     * @author       chenxiaogang
     * @param $where
     * @return int
     */
    public function get_report_count($where)
    {
        $data = $this->alias( 'ol' )
            ->field( 'ol.principal' )
            ->join( 'system_loan_received_log rl ON ol.id=rl.opid', 'left' )
            ->where( $where )
            ->group( 'ol.principal' )
            ->select();
        $count = count( $data );
        return $count;
    }

}